//
// DateTimeFormatter.cpp
//
// $Id: //poco/1.4/Foundation/src/DateTimeFormatter.cpp#3 $
//
// Library: Foundation
// Package: DateTime
// Module:  DateTimeFormatter
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/Timestamp.h"
#include "Poco/Format.h"


namespace Poco {


void DateTimeFormatter::append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt)
{
	DateTimeFormatter::append(str, dateTime.m_dateTime, fmt, dateTime.tzd());
}


void DateTimeFormatter::append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
	std::string::const_iterator it  = fmt.begin();
	std::string::const_iterator end = fmt.end();
	while (it != end)
	{
		if (*it == '%')
		{
			if (++it != end)
			{
				switch (*it)
				{
				case 'w': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
				case 'W': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
				case 'b': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break;
				case 'B': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
				case 'd': str += Poco::format("%02d",dateTime.day()); break;
				case 'e': str += Poco::format("%d",dateTime.day()); break;
				case 'f': str += Poco::format("%2d",dateTime.day()); break;
				case 'm': str += Poco::format("%02d",dateTime.month()); break;
				case 'n': str += Poco::format("%d",dateTime.month()); break;
				case 'o': str += Poco::format("%2d",dateTime.month()); break;
				case 'y': str += Poco::format("%2d", dateTime.year() % 100); break;
				case 'Y': str += Poco::format("%4d", dateTime.year()); break;
				case 'H': str += Poco::format("%02d",dateTime.hour()); break;
				case 'h': str += Poco::format("%02d",dateTime.hourAMPM()); break;
				case 'a': str.append(dateTime.isAM() ? "am" : "pm"); break;
				case 'A': str.append(dateTime.isAM() ? "AM" : "PM"); break;
				case 'M': str += Poco::format("%02d",dateTime.minute()); break;
				case 'S': str += Poco::format("%02d", dateTime.second()); break;
				case 's': str += Poco::format("%02d",dateTime.second()); 
				          str += '.'; 
				          str += Poco::format("%06d",dateTime.millisecond()*1000 + dateTime.microsecond()); 
				          break;
				case 'i': str += Poco::format("%03d", dateTime.millisecond()); break;
				case 'c': str += Poco::format("%d",dateTime.millisecond()/100); break;
				case 'F': str += Poco::format("%06d", dateTime.millisecond()*1000 + dateTime.microsecond()); break;
				case 'z': tzdISO(str, timeZoneDifferential); break;
				case 'Z': tzdRFC(str, timeZoneDifferential); break;
				default:  str += *it;
				}
				++it;
			}
		}
		else str += *it++;
	}
}


void DateTimeFormatter::append(std::string& str, const Timespan& timespan, const std::string& fmt)
{
	std::string::const_iterator it  = fmt.begin();
	std::string::const_iterator end = fmt.end();
	while (it != end)
	{
		if (*it == '%')
		{
			if (++it != end)
			{
				switch (*it)
				{
				case 'd': str += Poco::format("%d",timespan.days()); break;
				case 'H': str += Poco::format("%02d",timespan.hours()); break;
				case 'h': str += Poco::format("%d",timespan.totalHours()); break;
				case 'M': str += Poco::format("%02d", timespan.minutes()); break;
				case 'm': str += Poco::format("%d",timespan.totalMinutes()); break;
				case 'S': str += Poco::format("%02d",timespan.seconds()); break;
				case 's': str += Poco::format("%d",timespan.totalSeconds()); break;
				case 'i': str += Poco::format("%3d",timespan.milliseconds()); break;
				case 'c': str += Poco::format("%d",timespan.milliseconds()/100); break;
				case 'F': str += Poco::format("%06d",timespan.milliseconds()*1000 + timespan.microseconds()); break;
				default:  str += *it;
				}
				++it;
			}
		}
		else str += *it++;
	}
}


void DateTimeFormatter::tzdISO(std::string& str, int timeZoneDifferential)
{
	if (timeZoneDifferential != UTC)
	{
		if (timeZoneDifferential >= 0)
		{
			str += '+';
			str += Poco::format("%02d",timeZoneDifferential/3600);
			str += ':';
			str += Poco::format("%02d", (timeZoneDifferential%3600)/60);
		}
		else
		{
			str += '-';
			str += Poco::format("%02d", -timeZoneDifferential/3600);
			str += ':';
			str += Poco::format("%02d", (-timeZoneDifferential%3600)/60);
		}
	}
	else str += 'Z';
}


void DateTimeFormatter::tzdRFC(std::string& str, int timeZoneDifferential)
{
	if (timeZoneDifferential != UTC)
	{
		if (timeZoneDifferential >= 0)
		{
			str += '+';
			str += Poco::format("%02d", timeZoneDifferential/3600);
			str += Poco::format("%02d", (timeZoneDifferential%3600)/60);
		}
		else
		{
			str += '-';
			str += Poco::format("%02d", -timeZoneDifferential/3600);
			str += Poco::format("%02d", (-timeZoneDifferential%3600)/60);
		}		
	}
	else str += "GMT";
}


} // namespace Poco
